<template>
	<view class="wrap">
		<v-top-header />
		<v-menu />
		<u-tabs :list="tabList" :is-scroll="false" :current="currentTab" @change="changeTab"></u-tabs>
		<view class="condition-block">
			<view class="item-block" @click="showTime=true">
				<u-icon name="calendar"></u-icon>
				<view class="txt">{{ startTime ? startTime : '请选择开始时间' }}</view>
			</view>
			<u-line direction="col" color="red" length="30" />
			<view class="item-block" @click="showDay=true">
				<u-icon name="clock"></u-icon>
				<view class="txt">{{ hours ? hours + ' 小时' : '请选择时长' }}</view>
			</view>
		</view>
		<u-picker v-model="showTime" mode="time" :params="pickerParams" @confirm="confirmTime"></u-picker>
		<u-select v-model="showDay" :list="hourList" @confirm="confirmHours"></u-select>
		<view v-show="currentTab === 0">
			<view class="category-items">
				<view v-for="(item,index) in categoryList" :key="index" @click="changeSemulationCategory(item.label)"
					:class="['category-item',simulationCategoryLabel === item.label?'category-item-active':'']">
					{{item.label}}
				</view>
			</view>
			<view v-if="semulationDataList.length > 0">
				<view class="charts-box">
					<qiun-data-charts type="line" :chartData="chartData" :disableScroll="false" :opts="opts"
						:tapLegend="true" :ontouch="true" :onmovetip="true">
					</qiun-data-charts>
				</view>
				<u-table>
					<u-tr>
						<u-th>发生时间</u-th>
						<u-th>数值</u-th>
					</u-tr>
					<u-tr v-for="(item,index) in semulationDataList" :key="index">
						<u-td>{{item.t}}</u-td>
						<u-td>{{item.v}}</u-td>
					</u-tr>
				</u-table>
			</view>
			<view class="empty-block" v-else>
				<u-empty></u-empty>
			</view>
		</view>
		<!-- {{dataList}} -->
		<view v-show="currentTab === 1 || currentTab === 2 || currentTab === 3">
			<view v-if="dataList.length > 0">
				<u-card v-for="(item,index) in dataList" :key="index" :title="item.createTime"
					:sub-title="(index+1) + ''">
					<view class="card-block" slot="body">
						<view class="card-item" v-for="(subItem,subIndex) in item.items" :key="subIndex">
							<view>{{subItem.title}}</view>
							<view>{{subItem.value}}</view>
						</view>
					</view>
				</u-card>
			</view>
			<view class="empty-block" v-else>
				<u-empty></u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	import vTopHeader from '@/components/v-top-header.vue'
	import vMenu from '@/components/v-menu.vue'
	export default {
		components: {
			vTopHeader,
			vMenu
		},
		data() {
			return {
				showTime: false,
				showDay: false,
				pickerParams: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				startTime: null,
				hours: null,
				hourList: [],
				currentTab: 0,
				tabList: [{
					name: '模拟量'
				}, {
					name: '控制方式'
				}, {
					name: '报警信息'
				}, {
					name: '掘进机动作'
				}],
				simulationCategoryLabel: null,
				categoryList: [],
				chartData: {
					categories: [
						// '00:00',
						// '03:00',
						// '06:00',
						// '09:00',
						// '12:00',
						// '15:00',
						// '18:00',
						// '21:00',
						// '23:00'
					],
					"series": [{
						// "name": "电压V",
						"name": null,
						"data": [
							// 35,
							// 8,
							// 25,
							// 37,
							// 4,
							// 20,
							// 37,
							// 24,
							// 37
						]
					}]
				},
				dataList: [],
				semulationDataList: [],
				opts: {
					"type": "line",
					"canvasId": "",
					"canvas2d": false,
					"background": "none",
					"animation": true,
					"timing": "easeOut",
					"duration": 1000,
					"color": [
						"#1890FF",
						"#91CB74",
						"#FAC858",
						"#EE6666",
						"#73C0DE",
						"#3CA272",
						"#FC8452",
						"#9A60B4",
						"#ea7ccc"
					],
					"padding": [
						15,
						10,
						0,
						15
					],
					"rotate": false,
					"errorReload": true,
					"fontSize": 13,
					"fontColor": "#666666",
					"enableScroll": false,
					"touchMoveLimit": 60,
					"enableMarkLine": false,
					"dataLabel": false,
					"dataPointShape": false,
					"dataPointShapeType": "solid",
					"tapLegend": true,
					"xAxis": {
						"disabled": false,
						"axisLine": true,
						"axisLineColor": "#CCCCCC",
						"calibration": false,
						"fontColor": "#666666",
						"fontSize": 12,
						"rotateLabel": false,
						"labelCount": 6,
						"itemCount": 10,
						"boundaryGap": "center",
						"disableGrid": true,
						"gridColor": "#CCCCCC",
						"gridType": "solid",
						"dashLength": 4,
						"gridEval": 1,
						"scrollShow": false,
						"scrollAlign": "left",
						"scrollColor": "#A6A6A6",
						"scrollBackgroundColor": "#EFEBEF",
						"format": ""
					},
					"yAxis": {
						"disabled": false,
						"disableGrid": false,
						"splitNumber": 2,
						"gridType": "dash",
						"dashLength": 2,
						"gridColor": "#CCCCCC",
						"padding": 10,
						"showTitle": false,
						"data": []
					},
					"legend": {
						"show": true,
						"position": "bottom",
						"float": "center",
						"padding": 5,
						"margin": 5,
						"backgroundColor": "rgba(0,0,0,0)",
						"borderColor": "rgba(0,0,0,0)",
						"borderWidth": 0,
						"fontSize": 13,
						"fontColor": "#666666",
						"lineHeight": 11,
						"hiddenColor": "#CECECE",
						"itemGap": 10
					},
					"extra": {
						"line": {
							"type": "straight",
							"width": 2
						},
						"tooltip": {
							"showBox": true,
							"showArrow": true,
							"showCategory": false,
							"borderWidth": 0,
							"borderRadius": 0,
							"borderColor": "#000000",
							"borderOpacity": 0.7,
							"bgColor": "#000000",
							"bgOpacity": 0.7,
							"gridType": "solid",
							"dashLength": 4,
							"gridColor": "#CCCCCC",
							"fontColor": "#FFFFFF",
							"splitLine": true,
							"horizentalLine": false,
							"xAxisLabel": false,
							"yAxisLabel": false,
							"labelBgColor": "#FFFFFF",
							"labelBgOpacity": 0.7,
							"labelFontColor": "#666666"
						},
						"markLine": {
							"type": "solid",
							"dashLength": 4,
							"data": []
						}
					}
				}
			}
		},
		watch: {
			currentTab(newVal, oldVal) {
				console.log('currentTab--------------')
				if (newVal === 0) {
					this.loadSemulationVariableList()
				} else if (newVal === 1) {
					this.loadHisDataList(2);
				} else if (newVal === 2) {
					this.loadHisDataList(3);
				} else if (newVal === 3) {
					this.loadHisDataList(4)
				}
			},
			vuex_device(newVal, oldVal) {
				console.log('vuex_device=============')
				if (this.currentTab === 0) {
					this.loadSemulationVariableList()
				} else if (newVal === 1) {
					this.loadHisDataList(2);
				} else if (newVal === 2) {
					this.loadHisDataList(3);
				} else if (newVal === 3) {
					this.loadHisDataList(4)
				}
			},
			simulationCategoryLabel(newVal, oldVal) {
				this.loadSemulationVariableHisDataList(newVal)
			},
			startTime(newVal, oldVal) {
				if (this.currentTab === 0) {
					this.loadSemulationVariableHisDataList()
				} else if (this.currentTab === 1) {
					this.loadHisDataList(2);
				} else if (this.currentTab === 2) {
					this.loadHisDataList(3);
				} else if (this.currentTab === 3) {
					this.loadHisDataList(4)
				}
			},
			hours(newVal, oldVal) {
				if (this.currentTab === 0) {
					this.loadSemulationVariableHisDataList()
				} else if (this.currentTab === 1) {
					this.loadHisDataList(2);
				} else if (this.currentTab === 2) {
					this.loadHisDataList(3);
				} else if (this.currentTab === 3) {
					this.loadHisDataList(4)
				}
			}
		},
		created() {
			this.initHourList()
			this.loadSemulationVariableHisDataList()
		},
		methods: {
			initHourList() {
				for (var i = 1; i <= 8; i++) {
					this.hourList.push({
						label: i + '小时',
						value: i
					})
				}
			},
			changeTab(index) {
				this.currentTab = index
			},
			loadHisDataList(reqType) {
				this.dataList = []
				if (this.vuex_device === null || this.vuex_device.value === null || this.startTime === null || this
					.hours === null) {
					return;
				}
				console.log('loadHisDataList')
				this.$u.api.queryHisDataList({
					deviceId: this.vuex_device.value,
					reqType: reqType,
					startTime: encodeURI(this.startTime),
					duration: this.hours
					// startTime: encodeURI('2022-01-01 08:01:01'),
					// duration: 8
				}).then(res => {
					if (res.result === 1) {
						if (res.data.length > 0) {
							let recordCount = res.data[0].cnt2 //记录数量
							let valueCount = res.data[0].cnt1 //值类型数量
							for (var i = 1; i <= recordCount; i++) {
								let item = res.data[i]
								let temp = []
								for (var j = 1; j <= valueCount; j++) {
									temp.push({
										title: res.data[0]['name' + j],
										value: item['value' + j]
									})
								}
								this.dataList.push({
									createTime: item['time' + i],
									items: temp
								})
							}
						}
					}
				}).catch(e => {
					console.log(e)
				})
			},
			loadSemulationVariableList() {
				this.$u.api.querySemulationVariableList({
					deviceId: this.vuex_device.value
				}).then(res => {
					console.log('1111111111111111', res)
					if (res.result === 1) {
						let _list = []
						for (var key in res) {
							if (key.indexOf('name') !== -1) {
								_list.push({
									key: key,
									label: res[key]
								})
							}
						}
						this.categoryList = _list
						if (this.categoryList.length > 0) {
							this.simulationCategoryLabel = this.categoryList[0].label
						}
					}
				})
			},
			confirmTime(data) {
				this.startTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
					':' + data.second
			},
			confirmHours(data) {
				this.hours = data[0].value
			},
			changeSemulationCategory(categoryLabel) {
				this.simulationCategoryLabel = categoryLabel
			},
			loadSemulationVariableHisDataList() {
				console.log('loadSemulationVariableHisDataList-------------')
				this.semulationDataList = []
				if (this.vuex_device === null || this.vuex_device.value === null || this.startTime === null || this
					.hours === null) {
					console.log('invalid params')
					return;
				}
				console.log('---->', this.vuex_device.value, this.simulationCategoryLabel, this.startTime, this.hours)
				this.$u.api.querySemulationVariableHisDataList({
					deviceId: this.vuex_device.value,
					variableName: encodeURI(this.simulationCategoryLabel),
					startTime: encodeURI(this.startTime),
					duration: this.hours
					// startTime: encodeURI('2022-02-19 19:01:01'),
					// duration: 8
				}).then(res => {
					console.log('22222222222222222', res)
					if (res.result === 1) {
						this.semulationDataList = res.data.list
						this.chartData.categories = res.data.list.map(item => item.t.substring(9, ))
						this.opts.xAxis.itemCount = res.data.list.length
						this.chartData.series[0].data = res.data.list.map(item => item.v)
						this.chartData.series[0].name = this.simulationCategoryLabel + ' ' + res.data.dw
					}
				}).catch(e => {
					console.log(e)
				})
			}
		}
	}
</script>
<style>
	body,
	page {
		background: #fafafa;
	}
</style>
<style lang="scss" scoped>
	.wrap {
		.condition-block {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 40rpx;
			background-color: #ebebeb;

			.item-block {
				display: flex;
				align-items: center;


				.txt {
					margin-left: 10rpx;
				}
			}
		}

		.category-items {
			display: flex;
			flex-wrap: wrap;

			.category-item {
				background-color: #ebebeb;
				margin: 6px 10upx 0 10upx;
				padding: 4upx 8upx;
				border-radius: 10upx;
			}

			.category-item-active {
				background-color: #EE6666;
				color: #fff;
			}
		}

		.charts-box {
			display: flex;
			align-items: center;
			justify-content: center;
			// width: 90vw;
			// padding: 0 40upx;
			margin-bottom: 60upx;
		}

		.card-block {
			.card-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 10upx 0;
			}
		}

		.empty-block {
			margin-top: 20%;
		}
	}
</style>
